package com.ruoyi.project.examine.service.impl;


import com.ruoyi.project.examine.mapper.CheckMapper;
import com.ruoyi.project.examine.domain.CheckMode;
import com.ruoyi.project.examine.service.CheckService;
import com.ruoyi.project.system.newfunction.domain.Exam_cost;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Service
public class  CheckServiceImpl implements CheckService {
    //保存文件的地址
    private final String dir="D:/uploadFiles";
    @Autowired
    private CheckMapper checkMapper;
    /**
     * 科室查询
     * @return
     */
    @Override
    public List<Exam_cost> findCheckName() {
        return checkMapper.selectCheckName();
    }

    /**
     * 新开检查
     * @param checkCode 检查单号
     * @return
     */
    @Override
    public List<CheckMode> findCheckStart(String examName,String checkCode) {
        List<CheckMode> checkModes = checkMapper.selectCheckCode(examName, checkCode);
        return checkCode!=null?checkModes:null;
    }

    /**
     * 修改检查状态
     * @param checkCode
     * @return
     */
    @Override
    public int changeCheck(String checkCode) {
        return checkMapper.updatestate1(checkCode);
    }

    /**
     * 录入检查结果前的数据回显
     * @param examName
     * @param checkCode
     * @return
     */
    @Override
    public List<CheckMode> findResult1(String examName,String checkCode) {
        return checkMapper.selectResult1(examName, checkCode);
    }

    /**
     * 录入检查结果
     * 并修改检查状态和添加检查图片
     * @param checkCode
     * @param checkResult
     * @return true 数据修改成功  false 数据修改失败
     */
    @Override
    @Transactional
    public boolean change(String checkCode, String checkResult, String filename,String filesite) {
        int updateresult = checkMapper.updateresult(checkCode, checkResult,filename,filesite);
        int i = checkMapper.updatestate2(checkCode);

        //int insert = checkMapper.insert(originalFilename, randomFileName, checkCode);
        if (updateresult>0&&i>0){
            return true;
        }
        return false;
    }

    /**
     * 查看检查结果
     * @param examName
     * @param checkCode
     * @return
     */
    @Override
    public List<CheckMode> checkresult(String examName, String checkCode) {
        return checkMapper.selectResult2(examName, checkCode);
    }

    @Override
    public byte[] getImage(String filename) {
        System.out.println(filename);

        File file= new File(new File(dir),filename);

        FileInputStream inputStream=null;
        byte[] datas=null;

        try {
            inputStream = new FileInputStream(file);

            datas=new byte[inputStream.available()];
            inputStream.read(datas,0,datas.length);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            datas=null;
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("datas"+datas);

        return datas;
    }

    @Override
    public String upload(MultipartFile file) {
        //获取文件的原始名称
        String originalFilename = file.getOriginalFilename();
        //获取文件的后缀
        String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
        String string = UUID.randomUUID().toString()+extName;
        File file1 = new File(dir, string);
        try {
            file.transferTo(file1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return string;
    }

}
